package com.xuelang;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiwen.zxg
 * @since 2024/5/23
 */
//@SpringBootTest(classes = {SpringbootApp.class})
//@RunWith(SpringRunner.class)
//@PropertySource({"spring.config.location=classpath:application-local.yml"})
public class Test {

    String testSchema = "[\n" +
            "          {\n" +
            "            field: 'componentLabel',\n" +
            "            label: '',\n" +
            "            component: 'Input',\n" +
            "            vShow: false,\n" +
            "          },\n" +
            "          {\n" +
            "            field: 'codeRepository',\n" +
            "            label: '代码仓库',\n" +
            "            component: 'RichSelect',\n" +
            "            colProps: {\n" +
            "              span: 24,\n" +
            "            },\n" +
            "            labelWidth: 80,\n" +
            "            componentProps: ({ formModel }: any) => ({\n" +
            "              placeholder: '请选择代码仓库',\n" +
            "              optionMode: 'api',\n" +
            "              params: { componentLabel: formModel.componentLabel },\n" +
            "              api: '/pipeline/component/repo/dict',\n" +
            "              fieldNames: {\n" +
            "                value: 'id',\n" +
            "                label: 'text',\n" +
            "              },\n" +
            "            }),\n" +
            "            required: true,\n" +
            "            dynamicRules: () => [{ required: true, message: '请选择代码仓库' }],\n" +
            "          },\n" +
            "          {\n" +
            "            colProps: {\n" +
            "              span: 24,\n" +
            "            },\n" +
            "            component: 'RichSelect',\n" +
            "            field: 'token',\n" +
            "            label: 'token',\n" +
            "            componentProps: ({ formModel }: any) => ({\n" +
            "              placeholder: '请选择token',\n" +
            "              optionMode: 'api',\n" +
            "              params: { componentLabel: formModel.componentLabel },\n" +
            "              api: '/pipeline/component/token/dict',\n" +
            "              fieldNames: {\n" +
            "                value: 'id',\n" +
            "                label: 'text',\n" +
            "              },\n" +
            "            }),\n" +
            "            labelWidth: 80,\n" +
            "            required: true,\n" +
            "            dynamicRules: () => [{ required: true, message: '请选择token' }],\n" +
            "          },\n" +
            "          {\n" +
            "            colProps: {\n" +
            "              span: 24,\n" +
            "            },\n" +
            "            component: 'RichSelect',\n" +
            "            field: 'branch',\n" +
            "            label: '分支',\n" +
            "            componentProps: ({ formModel }: any) => ({\n" +
            "              placeholder: '请选择分支',\n" +
            "              optionMode: 'api',\n" +
            "              params: { codeRepository: formModel.codeRepository, token: formModel.token },\n" +
            "              api: '/pipeline/git/remote/branch/list/dict',\n" +
            "              showSearch: true,\n" +
            "              isBackendSearch: true,\n" +
            "              fieldNames: {\n" +
            "                value: 'id',\n" +
            "                label: 'text',\n" +
            "              },\n" +
            "            }),\n" +
            "            labelWidth: 80,\n" +
            "            required: true,\n" +
            "            dynamicRules: () => [{ required: true, message: '请选择分支' }],\n" +
            "          },\n" +
            "        ]\n";

    public static void main(String[] args) {

        String data = "2021-12-21T01:22:43.418913408Z";
        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        ZonedDateTime dateTime = ZonedDateTime.parse(data, formatter);

        System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); // 输出解析后的日期时间
        /*
        System.out.println(IdUtil.randomUUID());
        System.out.println(IdUtil.fastUUID());
        System.out.println(IdUtil.fastSimpleUUID());
        System.out.println(IdUtil.simpleUUID());
        System.out.println(IdUtil.getSnowflake().toString());
        System.out.println(IdUtil.getSnowflakeNextId());
        System.out.println(IdUtil.nanoId());
        System.out.println(IdUtil.objectId());
        System.out.println(IdUtil.getSnowflakeNextIdStr());
        System.out.println();
        System.out.println(StringUtils.isNotBlank("   "));
        System.out.println(StringUtils.isNotBlank("123"));
        System.out.println(StringUtils.isNotBlank("null"));
        generateTaskList();
         */
    }

//    @Autowired
//    TaskDefMapper taskDefMapper;
//    @Autowired
//    TaskDefController taskDefController;
//    @Autowired
//    PipelineController pipelineController;


    //@org.junit.Test
    public void insertTaskListToDB() {
        /*taskDefMapper.selectList(Wrappers.emptyWrapper());
        List<TaskType> taskTypeList = new ArrayList<>();
        ApiResult<List<TaskType>> result = ApiResult.success(taskTypeList);*/

        /*taskTypeList.forEach(taskType -> {
            String typeCode = taskType.getTypeCode();
            String typeName = taskType.getTypeName();
            List<TaskInfo> taskInfoList = taskType.getTaskInfoList();
            taskInfoList.forEach(taskInfo -> {
                TaskDefDO taskDefDO = TaskDefDO.builder()
                        .uuid(IdUtil.fastSimpleUUID())
                        .name(taskInfo.getTaskName())
                        .refTag(taskInfo.getTaskCode())
                        .schemaConfig(JSON.toJSONString(taskInfo.getTaskFieldList()))
                        .taskDefType(typeCode)
                        .creatorId("13122222222")
                        .updaterId("13122222222")
                        .build();
                taskDefMapper.insert(taskDefDO);
            });
        });*/
        /*List<TaskType> taskTypeList = new ArrayList<>();
        generateTaskList(taskTypeList);
        ApiResult<List<TaskType>> result2 = ApiResult.success(taskTypeList);

        ApiResult<List<TaskTypeFormDTO>> result = taskDefController.conditionalTaskDefAssemblyList(new TaskDefQuery());
        System.out.println(JSON.toJSONString(result, SerializerFeature.PrettyFormat, SerializerFeature.SortField));
        System.out.println();
        System.out.println();
        System.out.println(JSON.toJSONString(result2, SerializerFeature.PrettyFormat, SerializerFeature.SortField));*/
        /*ApiResult<PipelineConfigDTO> pipelineConfigDTOApiResult = pipelineController.queryPipelineConfig("80e82e2a2d394d65a701ebb4d5e7689c");
        System.out.println(JSON.toJSONString(pipelineConfigDTOApiResult, SerializerFeature.PrettyFormat, SerializerFeature.SortField));*/
        /*TaskDefDO taskDefDO = TaskDefDO.builder()
                .uuid("11111")
                .name("11111")
                .refTag("11111")
                .schemaConfig(testSchema)
                .taskDefType("11111")
                .creatorId("13122222222")
                .updaterId("13122222222")
                .build();
        taskDefMapper.insert(taskDefDO);*/
    }

    private static void generateTaskList(List<TaskType> taskTypeList) {
        List<TaskInfo> pullCodeList = new ArrayList<>();
        List<TaskInfo> buildCodeList = new ArrayList<>();
        List<TaskInfo> buildImageList = new ArrayList<>();
        List<TaskInfo> scanCodeList = new ArrayList<>();
        List<TaskInfo> deployCodeList = new ArrayList<>();

        taskTypeList.add(TaskType.builder().typeCode("pull_code").typeName("拉取代码").taskInfoList(pullCodeList).build());
        taskTypeList.add(TaskType.builder().typeCode("build_code").typeName("代码构建").taskInfoList(buildCodeList).build());
        taskTypeList.add(TaskType.builder().typeCode("build_image").typeName("镜像构建").taskInfoList(buildImageList).build());
        taskTypeList.add(TaskType.builder().typeCode("scan_code").typeName("代码扫描").taskInfoList(scanCodeList).build());
        taskTypeList.add(TaskType.builder().typeCode("deploy_code").typeName("部署").taskInfoList(deployCodeList).build());

        List<TaskField> codeSourceList = new ArrayList<>();
        codeSourceList.add(TaskField.builder().field("codeRepository").label("代码仓库").editable(false).build());
        codeSourceList.add(TaskField.builder().field("token").label("token").editable(false).build());
        codeSourceList.add(TaskField.builder().field("branch").label("分支").component("Select").build());
        pullCodeList.add(TaskInfo.builder().taskCode("code_source").taskName("代码源").taskFieldList(codeSourceList).build());

        List<TaskField> javaBuildList = new ArrayList<>();
        javaBuildList.add(TaskField.builder().field("buildCommand").label("构建命令").component("CodeInputComponent").defaultValue("mvn").build());
        List<TaskField> nodejsBuildList = new ArrayList<>();
        nodejsBuildList.add(TaskField.builder().field("buildCommand").label("构建命令").component("CodeInputComponent").defaultValue("npm").build());
        buildCodeList.add(TaskInfo.builder().taskCode("java_build").taskName("JAVA构建").taskFieldList(javaBuildList).build());
        buildCodeList.add(TaskInfo.builder().taskCode("nodejs_build").taskName("NODEJS构建").taskFieldList(nodejsBuildList).build());

        List<TaskField> customImageList = new ArrayList<>();
        customImageList.add(TaskField.builder().field("dockerFilePath").label("dockerfile文件路径").required(false).defaultValue("dockerFile").build());
        customImageList.add(TaskField.builder().field("imageUrl").label("镜像仓库地址").description("默认使用 ***仓库，\n输入格式如registry.com/repository").build());
        customImageList.add(TaskField.builder().field("imageTag").label("镜像标签").required(false).description("不填默认规则\n生成： 分支+时间戳").build());
        List<TaskField> goImageList = new ArrayList<>();
        goImageList.add(TaskField.builder().field("buildCommand").label("构建命令").component("CodeInputComponent").defaultValue("go build").build());
        goImageList.add(TaskField.builder().field("dockerFilePath").label("dockerfile文件路径").defaultValue("dockerFile").build());
        goImageList.add(TaskField.builder().field("imageUrl").label("镜像仓库地址").description("输入格式如registry.com/repository").build());
        goImageList.add(TaskField.builder().field("imageTag").label("镜像标签").required(false).description("不填默认规则\n生成： 分支+时间戳").build());
        List<TaskField> javaImageList = new ArrayList<>();
        javaImageList.add(TaskField.builder().field("buildCommand").label("构建命令").component("CodeInputComponent").defaultValue("mvn").build());
        javaImageList.add(TaskField.builder().field("dockerFilePath").label("dockerfile文件路径").defaultValue("dockerFile").build());
        javaImageList.add(TaskField.builder().field("imageUrl").label("镜像仓库地址").description("输入格式如registry.com/repository").build());
        javaImageList.add(TaskField.builder().field("imageTag").label("镜像标签").required(false).description("不填默认规则\n生成： 分支+时间戳").build());
        List<TaskField> pythonImageList = new ArrayList<>();
        pythonImageList.add(TaskField.builder().field("dockerFilePath").label("dockerfile文件路径").defaultValue("dockerFile").build());
        pythonImageList.add(TaskField.builder().field("imageUrl").label("镜像仓库地址").description("输入格式如registry.com/repository").build());
        pythonImageList.add(TaskField.builder().field("imageTag").label("镜像标签").required(false).description("不填默认规则\n生成： 分支+时间戳").build());
        List<TaskField> nodejsImageList = new ArrayList<>();
        nodejsImageList.add(TaskField.builder().field("buildCommand").label("构建命令").component("CodeInputComponent").defaultValue("npm").build());
        nodejsImageList.add(TaskField.builder().field("dockerFilePath").label("dockerfile文件路径").defaultValue("dockerFile").build());
        nodejsImageList.add(TaskField.builder().field("imageUrl").label("镜像仓库地址").description("输入格式如registry.com/repository").build());
        nodejsImageList.add(TaskField.builder().field("imageTag").label("镜像标签").required(false).description("不填默认规则\n生成： 分支+时间戳").build());

        buildImageList.add(TaskInfo.builder().taskCode("custom_image").taskName("自定义镜像构建").taskFieldList(customImageList).build());
        buildImageList.add(TaskInfo.builder().taskCode("go_image").taskName("GO镜像构建").taskFieldList(goImageList).build());
        buildImageList.add(TaskInfo.builder().taskCode("java_image").taskName("JAVA镜像构建").taskFieldList(javaImageList).build());
        buildImageList.add(TaskInfo.builder().taskCode("python_image").taskName("PYTHON镜像构建").taskFieldList(pythonImageList).build());
        buildImageList.add(TaskInfo.builder().taskCode("nodejs_image").taskName("NODEJS镜像构建").taskFieldList(nodejsImageList).build());

        List<TaskField> javaScanList = new ArrayList<>();
        javaScanList.add(TaskField.builder().field("codeScan").label("扫描路径").defaultValue("./").build());
        javaScanList.add(TaskField.builder().field("alertInfo").label("红线信息").component("AlertInfoComponent").required(false).build());
        List<TaskField> goScanList = new ArrayList<>();
        goScanList.add(TaskField.builder().field("codeScan").label("扫描路径").defaultValue("./").build());
        goScanList.add(TaskField.builder().field("alertInfo").label("红线信息").component("AlertInfoComponent").required(false).build());
        List<TaskField> pythonScanList = new ArrayList<>();
        pythonScanList.add(TaskField.builder().field("codeScan").label("扫描路径").defaultValue("./").build());
        pythonScanList.add(TaskField.builder().field("alertInfo").label("红线信息").component("AlertInfoComponent").required(false).build());
        List<TaskField> nodejsScanList = new ArrayList<>();
        nodejsScanList.add(TaskField.builder().field("codeScan").label("扫描路径").defaultValue("./").build());
        nodejsScanList.add(TaskField.builder().field("alertInfo").label("红线信息").component("AlertInfoComponent").required(false).build());
        List<TaskField> personScanList = new ArrayList<>();
        personScanList.add(TaskField.builder().field("verifierMode").label("验证者方式").component("Select").editable(false).defaultValue("{\"orSign\":\"或签\"}").build());
        personScanList.add(TaskField.builder().field("verifierType").label("验证者类型").component("Select").build());
        personScanList.add(TaskField.builder().field("verifier").label("验证人").component("Select").build());
        personScanList.add(TaskField.builder().field("verifierRole").label("验证角色").component("Select").build());

        scanCodeList.add(TaskInfo.builder().taskCode("java_scan").taskName("JAVA代码扫描").taskFieldList(javaScanList).build());
        scanCodeList.add(TaskInfo.builder().taskCode("go_scan").taskName("GO代码扫描").taskFieldList(goScanList).build());
        scanCodeList.add(TaskInfo.builder().taskCode("python_scan").taskName("PYTHON代码扫描").taskFieldList(pythonScanList).build());
        scanCodeList.add(TaskInfo.builder().taskCode("nodejs_scan").taskName("NODEJS代码扫描").taskFieldList(nodejsScanList).build());
        scanCodeList.add(TaskInfo.builder().taskCode("person_scan").taskName("人工卡点").taskFieldList(personScanList).build());

        List<TaskField> scrollDeployList = new ArrayList<>();
        scrollDeployList.add(TaskField.builder().field("replicas").label("副本数").defaultValue("1").build());
        scrollDeployList.add(TaskField.builder().field("cpu").label("CPU").defaultValue("0.5").build());
        scrollDeployList.add(TaskField.builder().field("memory").label("内存").defaultValue("0.5").build());
        scrollDeployList.add(TaskField.builder().field("port").label("端口").defaultValue("80").build());
        scrollDeployList.add(TaskField.builder().field("domain").label("域名").description("*.nw.com").build());
        scrollDeployList.add(TaskField.builder().field("advancedConfig").label("高级配置").component("AdvancedConfigComponent").required(false).build());
        scrollDeployList.add(TaskField.builder().field("deployStrategy").label("部署策略").editable(false).defaultValue("滚动").build());
        scrollDeployList.add(TaskField.builder().field("deployEnv").label("部署环境").editable(false).build());
        List<TaskField> grayDeployList = new ArrayList<>();
        grayDeployList.add(TaskField.builder().field("replicas").label("副本数").defaultValue("1").build());
        grayDeployList.add(TaskField.builder().field("cpu").label("CPU").defaultValue("0.5").build());
        grayDeployList.add(TaskField.builder().field("memory").label("内存").defaultValue("0.5").build());
        grayDeployList.add(TaskField.builder().field("port").label("端口").defaultValue("80").build());
        grayDeployList.add(TaskField.builder().field("domain").label("域名").description("*.nw.com").build());
        grayDeployList.add(TaskField.builder().field("advancedConfig").label("高级配置").component("AdvancedConfigComponent").required(false).build());
        grayDeployList.add(TaskField.builder().field("deployStrategy").label("部署策略").editable(false).defaultValue("灰度分批").build());
        grayDeployList.add(TaskField.builder().field("batchesNum").label("分批次数").defaultValue("1").build());
        grayDeployList.add(TaskField.builder().field("deployEnv").label("部署环境").editable(false).build());

        deployCodeList.add(TaskInfo.builder().taskCode("scroll_deploy").taskName("滚动发布").taskFieldList(scrollDeployList).build());
        deployCodeList.add(TaskInfo.builder().taskCode("gray_deploy").taskName("灰度发布").taskFieldList(grayDeployList).build());

    }

}
